package com.ycode.bang.anbang.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.model.OrderDetil;
import com.ycode.bang.anbang.network.FinalHttpUtil;
import com.ycode.bang.anbang.utils.RequestMapDev;
import com.ycode.bang.anbang.utils.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 转报修原因
 */
public class CloseActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.edit_all)
    EditText mEdit;
    @InjectView(R.id.btn_againdeal)
    Button mBtnAd;
    @InjectView(R.id.title)
    TextView title;

    private OrderDetil orderDetil = new OrderDetil();
    private int orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_close);
        ButterKnife.inject(this);
        if (this.getIntent().getExtras() != null) {
            Bundle bundle = this.getIntent().getExtras();
            orderId = bundle.getInt("orderId");
        }
        initView();
    }

    public void initView() {
        title.setText("申请关闭");
        mBtnAd.setOnClickListener(this);
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_againdeal:
                if (TextUtils.isEmpty(mEdit.getText().toString().trim())) {
                    ToastUtil.show(this, "请填写原因！");
                }else {
                    applyClose();
                }
                break;
        }
    }

    /**
     * 申请关闭
     */

    private void applyClose() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        mAjaxParam.put("orderId", orderId + "");
        mAjaxParam.put("closeReason", mEdit.getText().toString().trim());
        FinalHttpUtil.post(RequestMapDev.ApplyClose, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        ToastUtil.show(CloseActivity.this, "申请成功！");
                        finish();
                    } else {
                        ToastUtil.show(CloseActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(CloseActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
//                ToastUtil.show(CloseActivity.this, strMsg);
            }
        });
    }
}
